	----------------------------------------------------------------------------------
	--
	-- scenetemplate.lua
	--
	----------------------------------------------------------------------------------
	local physics = require("physics")
	physics.start()
	physics.setGravity(0,0)
	local storyboard = require( "storyboard" )
	local scene = storyboard.newScene()
	local ButtonManager = require("button")
	local NodeManager = require("node")
	local xml = require( "xml" ).newParser()
	local inbox = xml:loadFile( "data.xml" )
	local message = {}
	local Json = require( "Json" )
	local saveGame = require("saveLoadGame")
	local Sprite_Manager = require("sprite_manager")
	----------------------------------------------------------------------------------
	-- 
	--	NOTE:
	--	
	--	Code outside of listener functions (below) will only be executed once,
	--	unless storyboard.removeScene() is called.
	-- 
	---------------------------------------------------------------------------------

	---------------------------------------------------------------------------------
	-- BEGINNING OF YOUR IMPLEMENTATION
	---------------------------------------------------------------------------------

	--Variable
	local imgBackground
	local _W,_H = display.contentWidth,display.contentHeight

	local gameGroup, bkGroup, excellentGroup ,goodGroup, comboGroup
	local initButton
	local nodeInit_time = 1000
	local nodes = {}
	local node_speed = 3
	local tPrevious = system.getTimer()
	local data
	local timeLine = {}
	local timeRun = 1
	local time_space = 0
	local time_save = 0
	local button1, button2, button3
	local playBtn, pauseBtn, off_musicBtn, on_musicBtn, win_result, levelUp_menu, bt_continue, result, pause_menu, fail_menu, on_soundBtn, off_soundBtn, icon_list
	local startListener, stopListener, run, spawnChar, replayBtn
	local level = 1
	local state = {}
	local starCount = 0
	local score = 0
	local music_name
	local bonus_excellent, bonus_good, check_bonus, combo_numb, combo
	local count_good = 0
	local count_excellent = 0
	local score_menu
	Sprite_Manager.initSprite()
	local text = 0
	local check_enter = 0

	local distance

	local icon_link = "images/icon/"
	local game_link = "images/game/"
	local sound_link = "sound/"

	local play_music
--Listener Function
	if(saveGame.loadData()~= nil) then
		state = saveGame.loadData()
	end
	for i = 1,#inbox.child do
		message[i] = inbox.child[i]
	end
	if(state ~= nil ) then
		level = tonumber(state[1])
	end
	local bt_id = 1
	data = message[level].child[3].value
	music_name = message[level].child[2].value
	timeLine = Json.Decode(data);
	play_music = audio.loadStream(sound_link..music_name..".mp3" )
	run = function(event)
		if GAME_STATE == "run" then
			spawnNode(event)
		end
	end
	initButton = function()
		button1 = ButtonManager.newButton({x = _W*(0.35*0) + 70, y = _H*.85, id = "button1"})
		button1:init()
		gameGroup:insert(button1)

		button2 = ButtonManager.newButton({x = _W*(0.35*1) + 70, y = _H*.85, id = "button2"})
		button2:init()
		gameGroup:insert(button2)

		button3 = ButtonManager.newButton({x = _W*(0.35*2) + 70, y = _H*.85, id = "button3"})
		button3:init()
		gameGroup:insert(button3)
		
		button1:addEventListener("touch",onclickBtn)
		button2:addEventListener("touch",onclickBtn)
		button3:addEventListener("touch",onclickBtn)
		
		pauseBtn = display.newImageRect(icon_link.."icon_pause.png",30,30)
		pauseBtn.x = _W*.7
		pauseBtn.y = _H*.1
		pauseBtn.id = "pause"
		bkGroup:insert(pauseBtn)

		playBtn = display.newImageRect(icon_link.."icon_play.png",30,30)
		playBtn.x = _W*.7
		playBtn.y = _H*.1
		playBtn.id = "play"
		playBtn.alpha = 0
		bkGroup:insert(playBtn)

		on_musicBtn = display.newImageRect(icon_link.."icon_music_on.png",30,30)
		on_musicBtn.x = _W*.8
		on_musicBtn.y = _H*.1
		on_musicBtn.id = "on_music"
		bkGroup:insert(on_musicBtn)

		off_musicBtn = display.newImageRect(icon_link.."icon_music_off.png",30,30)
		off_musicBtn.x = _W*.8
		off_musicBtn.y = _H*.1
		off_musicBtn.id = "off_music"
		off_musicBtn.alpha = 0
		bkGroup:insert(off_musicBtn)

		on_soundBtn = display.newImageRect(icon_link.."icon_sound_on.png",30,30)
		on_soundBtn.x = _W*.9
		on_soundBtn.y = _H*.1
		on_soundBtn.id = "on_sound"
		bkGroup:insert(on_soundBtn)

		off_soundBtn = display.newImageRect(icon_link.."icon_sound_off.png",30,30)
		off_soundBtn.x = _W*.9
		off_soundBtn.y = _H*.1
		off_soundBtn.id = "off_sound"
		off_soundBtn.alpha = 0
		bkGroup:insert(off_soundBtn)
	end

	spawnNode = function(event)
		time_space = event.time - tPrevious + time_save
		if time_space > timeLine[timeRun] then
			timeRun = timeRun + 1
			local picture, node_id
			local _X = math.random (1,3)
			local r1 = math.random (0,2)
			local r2 = math.random (0,2)
			local r3 = math.random (0,2)
			if(_X == 1 ) then
				_X = _W*.15
				picture = "note1"..r1
				node_id = "node1"
			elseif(_X == 2) then
				_X = _W*.5
				picture = "note2"..r2
				node_id = "node2"
			elseif(_X == 3) then
				_X = _W*.85
				picture = "note3"..r3
				node_id = "node3"
			end
			text = text + 1
			local node = NodeManager.newNode({x = _X, y = -30, speed = node_speed, picture = picture, id = node_id })
			node:init()
			nodes[#nodes + 1] = node
			gameGroup:insert(1,node)
			tPrevious = event.time
			time_save = 0
		end
		if(timeRun == #timeLine) then
			timeRun = 1
			audio.stop( play_music )
			pause()
			nextLevel()
		end
	end
	local function onSceneTouch( self, event )
		if event.phase == "began" then
			if (self.id == "myText") then

			end
			return true
		end
	end

	startListener = function()
		Runtime:addEventListener("enterFrame",run)

		pauseBtn.touch = buttonTouchListener
		pauseBtn:addEventListener("touch",pauseBtn)

		playBtn.touch = buttonTouchListener
		playBtn:addEventListener("touch",playBtn)

		on_musicBtn.touch = buttonTouchListener
		on_musicBtn:addEventListener("touch",on_musicBtn)

		off_musicBtn.touch = buttonTouchListener
		off_musicBtn:addEventListener("touch",off_musicBtn)

		on_soundBtn.touch = buttonTouchListener
		on_soundBtn:addEventListener("touch",on_soundBtn)

		off_soundBtn.touch = buttonTouchListener
		off_soundBtn:addEventListener("touch",off_soundBtn)
	end

	function stopListener()
		for i=#nodes,1,-1 do
			if nodes[i] then nodes[i]:stopListener() end
		end
		button1:removeEventListener("touch",onclickBtn)
		button2:removeEventListener("touch",onclickBtn)
		button3:removeEventListener("touch",onclickBtn)

		Runtime:removeEventListener("enterFrame",run)
		pauseBtn:removeEventListener("touch",pauseBtn)
		playBtn:removeEventListener("touch",playBtn)
		GAME_STATE = ""
	end
	-- Called when the scene's view does not exist:
	function scene:createScene( event )
		storyboard.removeAll()
		local screenGroup = self.view
		imgBackground = display.newImageRect(game_link.."background.jpg",_G.STAGE_WIDTH,_G.STAGE_HEIGHT )
		imgBackground:setReferencePoint( display.TopLeftReferencePoint )
		imgBackground.x = STAGE_LEFT
		imgBackground.y = STAGE_TOP
		imgBackground.id = "imgBackground"
		gameGroup = display.newGroup()
		bkGroup = display.newGroup()
		excellentGroup = display.newGroup()
		goodGroup = display.newGroup()
		comboGroup = display.newGroup()
		comboGroup:insert(excellentGroup)
		comboGroup:insert(goodGroup)
		gameGroup:insert(comboGroup)
		screenGroup:insert(imgBackground)
		screenGroup:insert(bkGroup)
		screenGroup:insert(gameGroup)
		local line = display.newImageRect(game_link.."khungnhac.png",490,54)
		line.x = _W/2
		line.y = _H/2 + 40
		icon_list = display.newImageRect(icon_link.."icon_list.png",30,30)
		icon_list.isVisible = false
		bt_continue = display.newImageRect(icon_link.."continue.png",30,30)
		bt_continue.isVisible = false
		replayBtn = display.newImageRect(icon_link.."icon_replay.png",30,30)
		replayBtn.isVisible = false
		pause_menu = display.newImageRect(icon_link.."pause_menu.png",250,250)
		pause_menu.isVisible = false
		win_result = display.newImageRect(icon_link.."win_result.png",250,250)
		win_result.isVisible = false
		levelUp_menu = display.newImageRect(icon_link.. "next_level.png",350,250)
		levelUp_menu.isVisible = false
		fail_menu = display.newImageRect(icon_link.. "fail.png",350,250)
		fail_menu.isVisible = false
		result = display.newText("", 0, 0, native.systemFont, 30)
		result.isVisible = false
		score_menu = display.newText("score = "..score, 0, 0, native.systemFont, 20)
		score_menu.x = 50
		score_menu.y = 15
		bkGroup:insert(line)
		initButton()
		gameGroup:insert(fail_menu)
		gameGroup:insert(win_result)
		gameGroup:insert(pause_menu)
		gameGroup:insert(levelUp_menu)
		gameGroup:insert(score_menu)
		gameGroup:insert(icon_list)
		gameGroup:insert(bt_continue)
		gameGroup:insert(replayBtn)
		gameGroup:insert(result)
		audio.play(play_music)

		-----------------------------------------------------------------------------
		--	CREATE display objects and add them to 'group' here.
		--	Example use-case: Restore 'group' from previously saved state.
		-----------------------------------------------------------------------------
	end

	-- Called immediately after scene has moved onscreen:
	function scene:enterScene( event )
		if(check_enter == 0) then
			local screenGroup = self.view
			startListener()
			GAME_STATE = "run"
			runTimer = timer.performWithDelay( 1000, countTime, 0 )
			check_enter = 1
		end
		-----------------------------------------------------------------------------
		--	INSERT code here (e.g. start timers, load audio, start listeners, etc.)
		-----------------------------------------------------------------------------
		--Add Listeners
	--	myPicture:addEventListener( "touch", myPicture)
	end

	-- Called when scene is about to move offscreen:
	function scene:exitScene( event )
		local screenGroup = self.view
		stopListener()
		cleanGroups ( bkGroup, 0 )
		cleanGroups ( gameGroup, 0 )
		-----------------------------------------------------------------------------
		
		--	INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.)

		-----------------------------------------------------------------------------
		--Remove Listeners
		--myText:removeEventListener( "touch", myText)
	--	myPicture:removeEventListener( "touch", myPicture)
	end

	-- Called prior to the removal of scene's "view" (display group)
	function scene:destroyScene( event )
		local screenGroup = self.view
		-----------------------------------------------------------------------------
		
		--	INSERT code here (e.g. remove listeners, widgets, save state, etc.)
		
		-----------------------------------------------------------------------------
		
	end

	---------------------------------------------------------------------------------
	-- END OF YOUR IMPLEMENTATION
	---------------------------------------------------------------------------------

	-- "createScene" event is dispatched if scene's view does not exist
	scene:addEventListener( "createScene", scene )

	-- "enterScene" event is dispatched whenever scene transition has finished
	scene:addEventListener( "enterScene", scene )

	-- "exitScene" event is dispatched before next scene's transition begins
	scene:addEventListener( "exitScene", scene )

	-- "destroyScene" event is dispatched before view is unloaded, which can be
	-- automatically unloaded in low memory situations, or explicitly via a call to
	-- storyboard.purgeScene() or storyboard.removeScene().
	scene:addEventListener( "destroyScene", scene )
	function cleanGroups ( curGroup, level )
		if curGroup.numChildren then
			while curGroup.numChildren > 0 do
				cleanGroups ( curGroup[curGroup.numChildren], level+1 )
			end
			if level > 0 then
				curGroup:removeSelf()
			end
		else
			curGroup:removeSelf()
			curGroup = nil
			return true
		end
	end
	---------------------------------------------------------------------------------
	buttonTouchListener = function(self,event)
		if event.phase == "began" then
			if self.id == "pause" and GAME_STATE == "run" then
				pauseBtn.alpha = 0
				playBtn.alpha = 1
				audio.pause(play_music)

				button1:removeEventListener("touch",onclickBtn)
				button2:removeEventListener("touch",onclickBtn)
				button3:removeEventListener("touch",onclickBtn)
				pause_menu.isVisible = true
				pause_menu.x = _W/2
				pause_menu.y = _H/2
				bt_continue.isVisible = true
				bt_continue.x = _W/2 - 50
				bt_continue.y = _H*.6
				replayBtn.isVisible = true
				replayBtn.x = _W/2 - 10
				replayBtn.y = _H*.6
				replayBtn:addEventListener("touch",replayGame)
				icon_list.isVisible = true
				icon_list.x = _W/2 + 30
				icon_list.y = _H*.6
				icon_list:addEventListener("touch",gameList)
				pause()
				time_save = time_space
--				GAME_STATE = "run"
				function resumeGame(event)
					if(GAME_STATE == "pause" and event.phase == "began") then
						playBtn.alpha = 0
						pauseBtn.alpha = 1
						GAME_STATE = "run"
						tPrevious = event.time
						audio.resume(play_music)
						pause_menu.isVisible = false
						bt_continue.isVisible = false
						icon_list.isVisible = false
						replayBtn.isVisible = false
						button1:addEventListener("touch",onclickBtn)
						button2:addEventListener("touch",onclickBtn)
						button3:addEventListener("touch",onclickBtn)
						replayBtn:removeEventListener("touch",replayGame)
						button1:startListener()
						button2:startListener()
						button3:startListener()
						for i = #nodes,1,-1 do
							if(nodes[i].y ~= nil) then
								nodes[i]:startListener()
							end
						end
					end
				end
				bt_continue:addEventListener("touch",resumeGame)
			end
			if self.id == "on_music" then
				on_musicBtn.alpha = 0
				off_musicBtn.alpha = 1
				audio.setVolume( 0.0 )
			elseif self.id == "off_music" then
				on_musicBtn.alpha = 1
				off_musicBtn.alpha = 0
				audio.setVolume( 1.0 )
			end

			if self.id == "on_sound" then
				on_soundBtn.alpha = 0
				off_soundBtn.alpha = 1
				audio.setVolume( 0.0 )
			elseif self.id == "off_sound" then
				on_soundBtn.alpha = 1
				off_soundBtn.alpha = 0
				audio.setVolume( 1.0 )
			end
		end
	end
	function onclickBtn(event)
		if(event.phase == "began") then
			for i = 1, #nodes do
				if(nodes[i].y~= nil) then
					if(nodes[i].y > _H/2 + 25 and nodes[i].y < _H/2 + 60) then
						if((event.target.id == "button1" and nodes[i].id == "node1" ) or
							(event.target.id == "button2" and nodes[i].id == "node2") or
							(event.target.id == "button3" and nodes[i].id == "node3")) then
							nodes[i]:runEffect()
							nodes[i]:destroy()
							score = score + 1
							function comboEvent()
								combo = display.newText("", 0, 0, native.systemFont, 50)
								if(nodes[i].y > _H/2 + 30 and nodes[i].y < _H/2 + 35) then
									combo.isVisible = false
									count_good = 0
									excellentGroup.isVisible = true
									bonus_excellent = display.newImageRect(game_link.."excellent.png",100,50)
									bonus_excellent.x = _W/2
									bonus_excellent.y = _H/4
									count_excellent = count_excellent + 1
									score = score + 2*count_good + 4*count_excellent - 1
									if(count_excellent > 1) then
										combo = display.newText("X"..count_excellent, 0, 0, native.systemFont, 50)
										combo.x = _W/2 + 100
										combo.y = _H/4
										combo.isVisible = true
										excellentGroup:insert(combo)
									end
									excellentGroup:insert(bonus_excellent)
								elseif(nodes[i].y > _H/2 + 30 and nodes[i].y < _H/2 + 40) then
									combo.isVisible = false
									count_excellent = 0
									goodGroup.isVisible = true
									bonus_good = display.newImageRect(game_link.."good.png",100,50)
									bonus_good.x = _W/2
									bonus_good.y = _H/4
									count_good = count_good + 1
									score = score + 2*count_good - 1
									if(count_good > 1) then
										combo = display.newText("X"..count_good, 0, 0, native.systemFont, 50)
										combo.isVisible = true
										combo.x = _W/2 + 100
										combo.y = _H/4
										goodGroup:insert(combo)
									end
									goodGroup:insert(bonus_good)
								else
									count_good = 0
									count_excellent = 0
								end
								function hideBonus()
									excellentGroup.isVisible = false
									goodGroup.isVisible = false
									combo.isVisible = false
								end
								for i = 1, #nodes do
									if(nodes[i].y~= nil) then
										if(nodes[i].y > _H/2 + 60) then
											count_good = 0
											count_excellent = 0
										end
									end
								end
								transition.to( comboGroup, { time = 300, onComplete = hideBonus} )
							end
							comboEvent()
						end
						score_menu:removeSelf()
						score_menu = display.newText("score = "..score, 0, 0, native.systemFont, 20)
						score_menu.x = 50
						score_menu.y = 15
						bkGroup:insert(score_menu)
					end
				end
			end
		end
	end

	function nextLevel()
		replayBtn.isVisible = true
		replayBtn.x = _W/2 -10
		replayBtn.y = _H*.65
		icon_list.isVisible = true
		icon_list.x = _W/2 + 30
		icon_list.y = _H*.65
		replayBtn:addEventListener("touch",replayGame)
		icon_list:addEventListener("touch",gameList)
		if(level < 3) then
			if(score*100/#timeLine > 90) then
				starCount = 3
			elseif(score*100/#timeLine > 80 and score*100/#timeLine < 90) then
				starCount = 2
			elseif(score*100/#timeLine > 50 and score*100/#timeLine < 80) then
				starCount = 1
			else
				starCount = 1
			end
			function continue( event)
				result.isVisible = false
				level = level + 1
				state[1] = level
				state[2] = 1
				saveGame.saveData(state)
				storyboard.gotoScene("load_game")
			end
			if(starCount > 0) then
				levelUp_menu.isVisible = true
				levelUp_menu.x = _W/2
				levelUp_menu.y = _H/2
				bt_continue.isVisible = true
				bt_continue.x = _W/2 - 50
				bt_continue.y = _H*.65
				bt_continue:addEventListener("touch",continue)
			else
				fail_menu.isVisible = true
				fail_menu.x = _W/2
				fail_menu.y = _H/2
			end
			result.isVisible = true
			result = display.newText("So sao dat duoc la: "..starCount, 0, 0, native.systemFont, 30)
			result.x = _W/2
			result.y = _H*.5
			gameGroup:insert(result)
		else
			level = 1
			state[1] = level
			state[2] = 1
			saveGame.saveData(state)
			win_result.isVisible = true
			win_result.x = _W/2
			win_result.y = _H/2
		end
	end
	function pause()
		GAME_STATE = "pause"
--		stopListener()
		button1:stopListener()
		button2:stopListener()
		button3:stopListener()
		audio.pause(play_music)
		for i = #nodes,1,-1 do
			if(nodes[i] ~= nil) then
				nodes[i]:stopListener()
			end
		end
	end

	function resume()
		GAME_STATE = "run"
--		startListener()
		button1:startListener()
		button2:startListener()
		button3:startListener()
		audio.resume(play_music)
		for i = #nodes,1,-1 do
			if(nodes[i].y ~= nil) then
				nodes[i]:startListener()
			end
		end
	end
	function replayGame(event)
--		GAME_STATE = "run"
		audio.stop( play_music )
		storyboard.gotoScene("load_game")
	end

	function gameList()
		print("Back to main")
	end
	return scene